package cn.exrick.manager.service.impl;

import cn.exrick.common.exception.XmallException;
import cn.exrick.common.pojo.ZTreeNode;
import cn.exrick.manager.dto.DtoUtil;
import cn.exrick.manager.mapper.TbOrganizationMapper;
import cn.exrick.manager.pojo.TbOrganization;
import cn.exrick.manager.pojo.TbOrganizationExample;
import cn.exrick.manager.service.OrganizationService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private TbOrganizationMapper organizationMapper;

    @Override
    public TbOrganization getOrganizationById(Long id) {
        TbOrganization organization = organizationMapper.selectByPrimaryKey(id);
        if(organization==null){
            throw new XmallException("通过id获取部门失败");
        }
        return organization;
    }

    @Override
    public List<ZTreeNode> getOrganizationList(int parentId) {
        List<TbOrganization> list = organizationMapper.getOrgListByParentId(new Long(parentId));

        //转换成ZtreeNode
        List<ZTreeNode> resultList=new ArrayList<>();

        for(TbOrganization tbOrganization:list){
            ZTreeNode node= DtoUtil.TbOrganization2ZTreeNode(tbOrganization);
            resultList.add(node);
        }

        return resultList;
    }

    @Override
    public int addOrganization(TbOrganization tbOrganization) {
        tbOrganization.setSortOrder(0);
        tbOrganization.setCreateTime(new Date());
        tbOrganization.setUpdateTime(new Date());
        if(organizationMapper.insert(tbOrganization)!=1){
            throw new XmallException("添加部门失败");
        }
        return 1;
    }

    @Override
    public int updateOrganization(TbOrganization tbOrganization) {
        TbOrganization old=getOrganizationById(tbOrganization.getId());
        tbOrganization.setCreateTime(old.getCreateTime());
        tbOrganization.setUpdateTime(new Date());

        if(organizationMapper.updateByPrimaryKey(tbOrganization)!=1){
            throw new XmallException("更新部门失败");
        }
        return 1;
    }

    @Override
    public void deleteOrganization(Long id) {
        deleteZTree(id);
    }

    @Override
    public void deleteZTree(Long id) {
        //查询该节点所有子节点
        List<ZTreeNode> node=getOrganizationList(Math.toIntExact(id));
        if(node.size()>0){
            //如果有子节点，遍历子节点
            for(int i=0;i<node.size();i++){
                deleteOrganization((long) node.get(i).getId());
            }
        }
        //没有子节点直接删除
        if(organizationMapper.deleteByPrimaryKey(id)!=1){
            throw new XmallException("删除部门失败");
        }
    }

    @Override
    public void updateOrganizationBalance(Long id, BigDecimal balance) {
        TbOrganization organization = new TbOrganization();
        organization.setId(id);
        organization.setBalance(balance);
        organization.setUpdateTime(new Date());
        organizationMapper.updateByPrimaryKeySelective(organization);
    }

    @Override
    public List<String> getChildOrganizationIds(Long parentId) {
        List<String> result = new ArrayList<>();
        result.add(parentId.toString());
        TbOrganizationExample example=new TbOrganizationExample();
        TbOrganizationExample.Criteria criteria=example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbOrganization> list = organizationMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(list)){
            for(TbOrganization organization : list){
                Long childId = organization.getId();
                result.add(childId.toString());
                if(organization.getIsParent()){
                    getChildOrganizationIds(childId);
                }
            }
        }
        return result;
    }

    @Override
    public List<TbOrganization> getBalanceNotEnoughOrgList() {
        return organizationMapper.getBalanceNotEnoughOrgList();
    }
}
